package com.pz.cloud.framework.volunteerTeam.model;

import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroup;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroupUser;
import com.pz.cloud.framework.business.volunteerTeam.entity.VolunteerTeam;
import com.pz.cloud.framework.business.volunteerTeam.entity.VolunteerTeamUser;
import com.pz.cloud.framework.business.volunteerTeam.service.IVolunteerTeamService;
import com.pz.cloud.framework.business.volunteerTeam.service.IVolunteerTeamUserService;
import com.pz.cloud.framework.business.workflow.enums.WorkflowEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.model.BaseImportModel;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 志愿者队伍成员导入
 * @author mzz
 */
@Component
public class VolunteerTeamUserImportModel extends BaseImportModel<VolunteerTeamUser> {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IVolunteerTeamService volunteerTeamService;
    @Autowired
    private IVolunteerTeamUserService volunteerTeamUserService;

    /**
     * 获取导入模板文件
     * @return
     */
    @Override
    protected  String getTemplateFilePath(){
        String path = "excelTemplate/import/volunteerTeamUser.xlsx";
        return path;
    }

    @Override
    protected ImportValidetor validateEmptyField() {
        ImportValidetor importValidetor = ImportValidetor.instanec().addValidate("teamName", "志愿者队伍名称")
                .addValidate("stdNo","用户学号");
        return importValidetor;
    }

    /**
     * 保存导入的数据
     */
    @Override
    public void saveData(VolunteerTeamUser data){
        String userNo = data.getStdNo();
        SysUser sysUser = sysUserService.getUserByName(userNo);
        if(sysUser==null){
            throw new JeecgBootException("学号“"+userNo+"”不存在！");
        }
        data.setUserId(sysUser.getId());
        String teamName = data.getTeamName();
        List<VolunteerTeam> teams = volunteerTeamService.findByName(teamName.trim());
        if(CollectionUtils.isEmpty(teams)){
            throw new JeecgBootException("志愿者队伍“"+teamName+"”不存在！");
        }
        data.setVolunteerTeamId(teams.get(0).getId());
        data.setActStatus(WorkflowEnum.ProcessStatus.流程结束.getStatus());
        volunteerTeamUserService.save(data);
    }


    /**
     * 导入模板名称
     * @return
     */
    @Override
    public String getDemoFileName(){
            return "志愿者队伍成员导入模板";
    }

}
